﻿using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Media;
using AvalonDock;

namespace Poet
{
    /// <summary>
    /// Manage application theme.
    /// </summary>
    public class AppThemeManager
    {
        /// <summary>
        /// Gets the name of the current theme.
        /// </summary>
        /// <param name="pszThemeFileName">Name of the PSZ theme file.</param>
        /// <param name="dwMaxNameChars">The dw max name chars.</param>
        /// <param name="pszColorBuff">The PSZ color buff.</param>
        /// <param name="dwMaxColorChars">The dw max color chars.</param>
        /// <param name="pszSizeBuff">The PSZ size buff.</param>
        /// <param name="cchMaxSizeChars">The CCH max size chars.</param>
        /// <returns>Returns S_OK if successful, otherwise an error code.</returns>
        [DllImport("uxtheme.dll", CharSet = CharSet.Auto)]
        public static extern int GetCurrentThemeName(
            StringBuilder pszThemeFileName,
            int dwMaxNameChars,
            StringBuilder pszColorBuff,
            int dwMaxColorChars,
            StringBuilder pszSizeBuff,
            int cchMaxSizeChars);

        /// <summary>
        /// Sets the default theme.
        /// </summary>
        public static void SetDefaultAvalonDockTheme()
        {
            var themeFileName = new StringBuilder(0x200);
            GetCurrentThemeName(themeFileName, themeFileName.Capacity, null, 0, null, 0);

            if (string.IsNullOrEmpty(themeFileName.ToString()))
            {
                ThemeFactory.ChangeTheme("classic");
            }
            else
            {
                ThemeFactory.ChangeColors(Colors.Silver);
            }
        }

        /// <summary>
        /// Enables the visual styles for WinForms controls.
        /// </summary>
        public static void EnableVisualStylesForWinFormsControls()
        {
            System.Windows.Forms.Application.EnableVisualStyles();
        }

        /// <summary>
        /// Sets the defaults.
        /// </summary>
        public static void SetDefaults()
        {
            EnableVisualStylesForWinFormsControls();
            SetDefaultAvalonDockTheme();
        }
    }
}
